define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cabinet/info/index',
                    add_url: 'cabinet/info/add',
                    edit_url: 'cabinet/info/edit',
                    del_url: 'cabinet/info/del',
                    multi_url: 'cabinet/info/multi',
                    table: 'cabinet_info',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {field: 'qrCode', title: __('二维码'), operate: false, formatter: Controller.api.formatter.qrCode},
                        {field: 'cabinet_type_id', title: __('Cabinet_type_id')},
                        {field: 'charge_place_id', title: __('Charge_place_id')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"-1":__('Status -1'),"-2":__('Status -2')}, formatter: Table.api.formatter.status},
                        {field: 'running_status', title: __('Running_status'), searchList: {"0":__('Running_status 0'),"1":__('Running_status 1'),"-1":__('Running_status -1')}, formatter: Table.api.formatter.status},
                        {field: 'type.name', title: __('Type.name')},
                        {field: 'type.image', title: __('Type.image'), formatter: Table.api.formatter.image},
                        {field: 'type.slot_number', title: __('Type.slot_number')},
                        {field: 'place.name', title: __('Place.name')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                qrCode:function(value, row, index){
                    var imgSrc = '/qrcode/build?text='+ row.name + '&label='+row.name+'%09&logo=1&labelhalign=0&labelvalign=3&foreground=%23ffffff&background=%23000000&size=300&padding=10&logosize=50&labelfontsize=14&errorcorrection=medium';
                    return '<a href="'+imgSrc+'" target="_blank"><img class="img-sm img-center" src="'+imgSrc+'"/></a>'
                }
            }
        }
    };
    return Controller;
});